package com.lxb.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lxb.msmservice.service.EduMsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Map;

/**
 * @author xuebin.li
 * @createTime 2022/12/19 20:44
 */
@Service
public class EduMsmServiceImpl implements EduMsmService {


    /**
     * 阿里云发送短信
     *
     * @param param
     * @param phone
     * @return
     */
    @Override
    public boolean send(Map<String, Object> param, String phone) {

        if (StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI5tFkRxhuMjmFDNPrtnHi", "cmaJlrU01IXw9V9MV256Jm1c5NKeq2");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);   //接收短信手机号
        request.putQueryParameter("SignName", "谷粒商城");  //签名名称
        request.putQueryParameter("TemplateCode", "SMS_264875513"); //验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 发送邮件
     *
     * @param email
     * @param code
     * @return
     */
    @Override
    public boolean sendEmail(String email, String code) {

        /**
         * @param send   -->> 发件人
         * @param password -->>  发件人授权码
         * @param Receive： -->> 收件人
         * @param Title -->> 标题
         * @param Content -->> 内容
         */
        final String send = "xbli333@foxmail.com";
        final String password = "zrmyqcxcgudridfg";
        String Receive = email;
        final String Title = "大学生校园在线教育服务平台——验证码";
        Object Content = "[大学生校园在线教育服务平台] 您的验证码为 <s>"+ code +"<s/> ，请勿泄露于他人！";
        Object Content1 = "" +
                "\n" +
                "\n" +
                "    <style type=\"text/css\">\n" +
                "        .qmbox #outlook a { padding: 0; }\n" +
                "        .qmbox .ReadMsgBody { width: 100%; }\n" +
                "        .qmbox .ExternalClass { width: 100%; }\n" +
                "        .qmbox .ExternalClass * { line-height:100%; }\n" +
                "        .qmbox body { margin: 0; padding: 0; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }\n" +
                "        .qmbox table, .qmbox td { border-collapse:collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; }\n" +
                "        .qmbox img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; }\n" +
                "        .qmbox p { display: block; margin: 13px 0; }\n" +
                "    </style>\n" +
                "    <style type=\"text/css\">\n" +
                "        @media only screen and (max-width:480px) {\n" +
                "            @-ms-viewport { width:320px; }\n" +
                "            @viewport { width:320px; }\n" +
                "        }\n" +
                "    </style>\n" +
                "    <style type=\"text/css\">\n" +
                "\n" +
                "    </style>\n" +
                "    <style type=\"text/css\">\n" +
                "        @media only screen and (min-width:480px) {\n" +
                "            .qmbox .mj-column-per-100 { width:100%!important; }\n" +
                "        }\n" +
                "    </style>\n" +
                "\n" +
                "\n" +
                "\n" +
                "    <div><div style=\"margin:0px auto;max-width:600px;\"><table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:0px;width:100%;\" align=\"center\" border=\"0\"><tbody><tr><td style=\"text-align:center;vertical-align:top;direction:ltr;font-size:0px;padding:20px 0px;\"><div class=\"mj-column-per-100 outlook-group-fix\" style=\"vertical-align:top;display:inline-block;direction:ltr;font-size:13px;text-align:left;width:100%;\"><table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"0\"><tbody><tr><td style=\"word-break:break-word;font-size:0px;padding:10px 25px;\" align=\"left\"><div class=\"class\" style=\"cursor:auto;color:#000000;font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:22px;text-align:left;\">\n" +
                "        <p>您好，Hello</p><p>\n" +
                "    </p><p>\n" +
                "        验证码为：";
        Object Content2 = "    </p><p>\n" +
                "        如果您没有相关操作，请直接忽略这封邮件。<br>\n" +
                "        You can ignore the email safely if you do not want to reset password.\n" +
                "    </p>\n" +
                "    </div></td></tr></tbody></table></div></td></tr></tbody></table></div></div>\n" +
                "\n" +
                "    <style type=\"text/css\">.qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {display: none !important;}</style>";

        Properties properties = new Properties();
        //协议
        properties.put("mail.transport.protocol", "smtp");
        //服务器
        properties.put("mail.smtp.host", "smtp.qq.com");
        //设置端口，不然无法部署到阿里云服务器，默认端口25，这个不行
//        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.auth", "true");
        //SSL加密传输
        properties.put("mail.smtp.ssl.enable", "true");
        //true-->>在控制台显示信息，false-->>不显示
        // 开启 Session 的 debug 模式，这样就可以查看程序发送 Email 的运行状态
        properties.put("mail.debug", "true");
        //得到回话对象
        Session session = Session.getInstance(properties);


        try {
            //1.获取邮件对象
            Message message = new MimeMessage(session);
            //发件人邮箱
            message.setFrom(new InternetAddress(send));
            //设置收件人
            message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(Receive) });
            //邮箱标题
            message.setSubject(Title);
            //邮箱内容
            message.setContent(Content1+""+ code +""+Content2,"text/html;charset=UTF-8");

            //2.创建连接服务对象
            Transport transport = session.getTransport();
            /**
             * @param 第一个参数为自己QQ邮箱
             * @param 第二个参数为授权码
             */
            transport.connect(send, password);
            // 发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            //关闭服务
            transport.close();
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
}
